package Leetcode第二期;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author : K k
 * @date : 12:25 2020/9/28
 */
public class 填充每个节点的下一个右侧节点指针II_117 {

    class Solution {
        public Nnode connect(Nnode root) {
            Queue<Nnode> queue=new LinkedList<>();
            if (root == null) {
                return null;
            }
            queue.offer(root);
            root.next=null;
            while (!queue.isEmpty()) {
                int size=queue.size();
                for (int i=0;i<size;i++){
                    Nnode head = queue.poll();
                    head.next=i==size-1?null:queue.peek();
                    if (head.left != null) {
                        queue.offer(head.left);
                    }
                    if (head.right!=null){
                        queue.offer(head.right);
                    }
                }
            }
            return root;
        }
    }
}
